# nums = [1,2,3,1]
# k = 3
nums = [1,2,3,1,2,3]
k = 2
def containsNearbyDuplicate(nums, k: int) -> bool:
    dictionary=dict()
    for i in range(len(nums)):
        if nums[i] not in dictionary:
            dictionary[nums[i]]=i
        else:
            if i-dictionary[nums[i]]<=k:
                return True
            else:
                dictionary[nums[i]]=i
    return False

print(containsNearbyDuplicate(nums,k))
